//
//  BaseController.swift
//  YiZhangGui
//
//  Created by Mac on 2018/1/16.
//  Copyright © 2018年 Mac. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
//import CNUIBaseKit

class BaseController: CN_BaseController {
    
    /// 推送类型
    /// 0: 更新视频审核提醒（关闭）1：门店缺货提醒功能
    var identity = "0"
    var remoteNotification: Dictionary<AnyHashable, Any>? = nil
    
    var returnKeyHandler: IQKeyboardReturnKeyHandler!
    var isWhiteNav = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        if self.navigationController != nil {
            self.cn_hidesNavigationBarHairline = true
            
            self.cn_barTitleColor = UIColor.white
            self.cn_barTitleFont = UIFont.cn_fontSize(ofSize: 16.5, weight: .medium)
           
            self.cn_backIndicatorImage = UIImage(named: "BackForAllVc")
            self.cn_tintColor = UIColor.white
            
            
            self.cn_barButtonItemTitleColor = UIColor.white
            self.cn_barButtonItemTitleFont = UIFont.cn_fontSize(ofSize: 14.0, weight: .regular)
            
            self.cn_barTintColor = UIColor(hexString: "0f80fc")
        }
        
        
        
        
        
    }
    
   
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.cn_StatusBarDarkMode = false
        
        if isWhiteNav {
            self.cn_barTitleColor = .black
            self.cn_StatusBarDarkMode = true
            self.cn_barButtonItemTitleColor = .black
            self.cn_tintColor = .black
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {        
        super.viewDidDisappear(animated)
        
        self.remoteNotification = nil
//        NotificationCenter.default.removeObserver(self)
        
        self.returnKeyHandler = nil
    }
    
    deinit {
            // 移除观察者
            NotificationCenter.default.removeObserver(self)
        }
    
    
    
    override func viewWillDisappear(_ animated: Bool) {
        
        super.viewWillDisappear(animated)
    }
    
    @objc func pushToVC(controller: BaseController) {
        self.cn_pushController(controller: controller, hideNavigationBar: false, statusBarDarkMode: false)
    }
    
  
    
}

// MARK: ----------------------------------------------------------------
//
//  拜访用到的方法
//
extension BaseController {
    
    // 判断是否登录超时
    func getRequestResult(status: Int, message: String) {
        if status == 401 {
            DispatchQueue.main.async {
                // 用户会话已失效，退出到登录页面
                self.view.showTips(title: "会话已失效，请重新登录")
                UserDefaultsManager.removeAllData()
                
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.8) {
                    // 直接从 AppDelegate 获取窗口
                    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                        let loginCon = CN_BaseNavController(rootViewController: Login_Con())
                        appDelegate.window?.rootViewController = loginCon
                    }
                }
            }
        } else if status == request_success {
            // 处理成功情况
        } else {
            DispatchQueue.main.async {
                self.view.showTips(title: message)
            }
        }
    }
    
}



